// pages/din/din.js
import http from "../../request/index"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: wx.getStorageSync('list') || [],
    users: wx.getStorageSync('addres') || {},
    pricee: 0,
  },
  //获取地址的
  getaddres() {
    let addres = wx.getStorageSync("addres")
    this.setData({
      users: addres
    })

  },
  //计算价钱的
  getprice() {
    let arr = wx.getStorageSync('list') || []
    let prac = 0
    arr.forEach(item => {
      if (item.checked == true) {
        prac += item.num * item.price
      }
    })
    this.setData({
      pricee: prac
    })
  },

  //支付按钮
  add() {
    let arr1 = this.data.list.filter(item => {
      return item.checked == true
    })
    let goods = []
    let stri = JSON.stringify(this.data.users);
    arr1.forEach(item => {
      goods.push({
        goods_id: item.id,
        goods_number: item.num,
        goods_price: item.price
      })
    })
    http("public/v1/my/orders/create", {
      method: "post",
      data: {
        order_price: this.data.pricee, //总价钱
        consignee_addr: stri,
        goods: goods
      }
    }).then(res => {
      let {
        data
      } = res
      if (data.meta.status == 200) {
        this.orders(data.message.order_number)
        this.getchkOrder(data.message.order_number)
        // wx.showToast({
        //   title: '支付成功',
        // })
        // let arr = wx.getStorageSync('list') || []
        // let arr2 = arr.filter(item => {
        //   return item.checked == false
        // })
        // wx.setStorageSync('list', arr2)
        // wx.switchTab({
        //   url: '../gwc/gwc',

        // })
        // wx.showToast({
        //   title: '支付成功',
        //   duration:2000
        // })
      }
    })
  },
  //支付
  orders(e) {
    http("public/v1/my/orders/req_unifiedorder", {
      method: "post",
      data: {
        order_number: e
      }
    }).then(res => {
      let {
        data
      } = res
      let as = data.message.pay
      if (data.meta.status == 200) {
        wx.requestPayment({
          timeStamp: as.timeStamp,
          nonceStr: as.nonceStr,
          package: as.package,
          signType: as.signType,
          paySign: as.paySign,
          success(res) {
            wx.showToast({
              title: '支付成功',
              icon: "success"
            })
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: "error"
            })
          }
        })
      }
    })
  },


  //订单编号的状态
  getchkOrder(e) {
    http("public/v1/my/orders/chkOrder", {
      method: "post",
      data: {
        order_number: e
      }
    }).then(res => {
      let {
        data
      } = res
      if (data.meta.status == 200) {}
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlist()
    this.getprice()
    this.getaddres()
  },
  getlist() {
    this.setData({
      list: wx.getStorageSync('list') || []
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getlist()
    this.getprice()
    this.getaddres()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})